﻿/**
* 模块名：设备维修项目
* 程序名: sbwxxm.js
* 设备档案
**/
var dev = dev || {};
dev.viewModel = dev.viewModel || {};

dev.viewModel.searchEdit = function (data) {
    var self = this;
    this.urls = data.urls;
    this.resx = data.resx;
    this.dataSource = data.dataSource;
    this.form = ko.mapping.fromJS(data.form);
    this.defaultRow = data.defaultRow;
    this.setting = data.setting;
    delete this.form.__ko_mapping__;

    this.grid = {
        size: { w: 239, h: 40 },
        url: "/api/Dev/Sbwxxm",
        queryParams: ko.observable(),
        pagination: true
    };

    this.gridEdit = new com.editGridViewModel(this.grid);
    this.grid.onDblClickRow = this.gridEdit.begin;
    this.grid.onClickRow = this.gridEdit.ended;

    var treeId = null;
    this.tree = {
        method: 'GET',
        url: '/api/Dev/Sbwxxm/GetTypes',
        queryParams: ko.observable(),
        loadFilter: function (d) {
            var filter = utils.filterProperties(d.rows || d, ['Sbwxlb_Lbbh as id', 'Sbwxlb_Lbmc as text', 'Sbwxlb_Sjbh as pid']);
            var data = utils.toTreeData(filter, 'id', 'pid', "children");
            return [{ id: '', text: '所有类别', children: data }];
        },
        onSelect: function (node) {
            treeId = node.id;
            self.CodeType(node.id);
        }
    };

    this.CodeType = ko.observable();
    this.CodeType.subscribe(function (value) {
        //查询 (关联树)
        self.grid.queryParams({ Sbwxxm_Lbbh: value });
    });

    this.refreshClick = function () {
        window.location.reload();
    };

    this.addClick = function () {
        if (treeId != null) {
            com.ajax({
                type: 'GET',
                url: '/api/Dev/sbwxxm/getnewcode/' + self.CodeType(), //url: self.urls.billno,
                success: function (d) {
                    var row = { Sbwxxm_Xmbh: d, Sbwxxm_Lbbh: treeId };
                    self.gridEdit.addnew(row);
                }
            });
        } else {
            com.message('warning', '请选择之后进行操作！');
        }
    };

    this.editClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请选择之后进行操作！');
        self.gridEdit.begin(row);
    };

    this.saveClick = function () {
        self.gridEdit.ended();
        var post = {};
        post.list = self.gridEdit.getChanges(self.setting.postListFields);
        if (com.formValidate()) {
            com.ajax({
                url: '/api/Dev/Sbwxxm/edit',
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', "保存成功");
                    self.searchClick();
                }
            });
        }
    };

    this.searchClick = function () {
        var param = ko.toJS(this.form);
        this.grid.queryParams(param);
    };

    this.deleteClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', self.resx.noneSelect);
        com.message('confirm', self.resx.deleteConfirm, function (b) {
            if (b) {
                com.ajax({
                    type: 'DELETE', url: self.urls.remove + row["Sbwxxm_Xmbh"], success: function () {
                        com.message('success', self.resx.deleteSuccess);
                        self.searchClick();
                    }
                });
            }
        });
    };

    this.typeClick = function () {
        com.dialog({
            title: "&nbsp;设备维修类别",
            iconCls: 'icon-node_tree',
            width: 600,
            height: 440,
            html: "#type-template",
            viewModel: function (w) {
                var that = this;
                this.grid = {
                    width: 586,
                    height: 360,
                    idField: '_id', //虚拟主键
                    treeField: 'Sbwxlb_Lbbh',
                    url: "/api/dev/Sbwxxm/gettypes",
                    queryParams: ko.observable(),
                    loadFilter: function (d) {
                        //采用这种写法
                        d = utils.copyProperty(d.rows || d, ["Sbwxlb_Lbbh", "IconClass"], ["_id", "iconCls"], false);
                        return utils.toTreeData(d, '_id', 'Sbwxlb_Sjbh', "children");
                    }
                };
                this.gridEdit = new com.editTreeGridViewModel(this.grid);
                this.grid.OnAfterCreateEditor = function (editors, row) {
                    if (!row._isnew) com.readOnlyHandler('input')(editors["Sbwxlb_Lbbh"].target, true);
                };
                this.grid.onDblClickRow = that.gridEdit.begin;
                this.grid.toolbar = [
                    { text: '新增', iconCls: 'icon-add1', handler: function () { that.gridEdit.addnew(); } }, '-',
                    { text: '删除', iconCls: 'icon-cross', handler: that.gridEdit.deleterow }
                ];
                this.confirmClick = function () {
                    that.gridEdit.ended();
                    if (that.gridEdit.isChangedAndValid()) {
                        var list = that.gridEdit.getChanges(['Sbwxlb_Lbbh', 'Sbwxlb_Lbmc', 'Sbwxlb_Sjbh', 'Sbwxlb_Remark']);
                        com.ajax({
                            url: '/api/Dev/Sbwxxm/edittype/',
                            data: ko.toJSON({ list: list }),
                            success: function (d) {
                                that.cancelClick();
                                self.tree.$element().tree('reload');
                                com.message('success', '保存成功！');
                            }
                        });
                    }
                };
                this.cancelClick = function () {
                    w.dialog('close');
                };
            }
        });
    };
};